import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
//import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

// 引入按纽权限js文件
//import '@/common/js/permission'
import './common/js/permission'  // 能点进去

// 全局引入百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  /* Visit http://lbsyun.baidu.com/apiconsole/key for details about app key. */
  ak: 'rQ0ZW5svMB0YGctf5ez7TSoLtNRDLbW6'
})

// 引入axios
import axios from 'axios';
// 配置axios的全局基本路径
axios.defaults.baseURL="http://localhost:8080";
// 配置全局属性
Vue.prototype.$http=axios;

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})


// 1.axios前置拦截器：每次通过axios发送请求之前，让所有的请求都携带token
axios.interceptors.request.use(config=> {
  // 从localStorage中获取token
  let token = localStorage.getItem("token");
  // 如果有值，将token设置到请求头
  if (token) {
    config.headers['token'] = token;
  }
  return config;
  },error => {
  Promise.reject(error);
})

// 2.axios后置拦截器：后端处理完axios请求之后，处理响应的数据
axios.interceptors.response.use(res=>{
  if (false === res.data.success && "noLogin" === res.data.msg) {
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("menus");
    localStorage.removeItem("permissionSns");
    router.push({path: '/login'});
    alert("请先登陆！");
  }
  if (false === res.data.success && "noPermission" === res.data.msg) {
   alert("无权访问！");
   return;
  }
  if (false === res.data.success && "timeout" === res.data.msg) {
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("menus");
    localStorage.removeItem("permissionSns");
    router.push({path: '/login'});
    alert("登陆过期！");
  }
  return res;
},error => {
  Promise.reject(error);
})

// 3.路由拦截器,只要是通过路由的请求都会执行下面的代码
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == '/register' ) {
    next();
  }else{  // 访问其他页面，通过logininfo判断是否登录过
    let logininfo = JSON.parse(localStorage.getItem('logininfo'));
    if(logininfo){
      next();
    }else{  // 访问的不是登录页面，也不是注册页面 - 未登录
      next({ path: '/login' })
    }
  }
})


// 处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  // 防止重复配置路由：5就是main.js中路由的个数 - 如果你的静态路由是6个这里要写成6
  if(router.options.routes.length>5){
    return;
  }
  let menus = localStorage.getItem('menus');
  // 将json字符串 - json对象
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  // 动态路由配置
  router.addRoutes(tempRouters);
}



// 处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  // 防止重复配置路由：5就是main.js中路由的个数 - 如果你的静态路由是6个这里要写成6
  if(router.options.routes.length>5){
    return;
  }
  let menus = localStorage.getItem('menus');
  // 将json字符串 - json对象
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  // 动态路由配置
  router.addRoutes(tempRouters);
}



// 【动态路由配置】
// 处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  // 防止重复配置路由：5就是main.js中路由的个数 - 如果你的静态路由是6个这里要写成6
  if(router.options.routes.length>5){
    return;
  }
  let menus = localStorage.getItem('menus');
  // 将json字符串 - json对象
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  // 动态路由配置
  router.addRoutes(tempRouters);
}



new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')







